package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.RequestAndResultCode;
import com.bjesc_b2c.app.appraiset.adapter.CarSeriesModelAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.orhanobut.logger.Logger;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.CarSeriesModelBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by lenovo on 2017/3/2.
 * 车系
 */

public class CarModelActivity extends BaseActivity implements MultiItemTypeAdapter.OnItemClickListener {
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.recycler_view)
    RecyclerView recyclerView;

    private List<CarSeriesModelBean> data;
    private CarSeriesModelAdapter adapter;
    private String seriesId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_series_model);
        ButterKnife.inject(this);
        tvTitle.setText("车型");
        seriesId = getIntent().getStringExtra("seriesId");
        getCarSeries();
    }

    private void getCarSeries() {
        HashMap<String, Object> build = new MapUtil()
                .put("series_id", seriesId)
                .build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(this, build).create(IApiActivityRequest.class).getCarModel()
                , new BaseSubscriber<TokenBean<List<CarSeriesModelBean>>>(this) {
                    @Override
                    public void onSuccess(TokenBean<List<CarSeriesModelBean>> o) {
                        if (o.getData() != null && o.getData().size() > 0) {
                            initViews(o.getData());
                        }

                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });

    }

    private void initViews(List<CarSeriesModelBean> data) {
        this.data = data;
        adapter = new CarSeriesModelAdapter(CarModelActivity.this, R.layout.layout_item_select_car_brand, data);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
    }


    @OnClick(R.id.iv_return)
    public void onClick() {
        finish();
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        BasicInfoActivity.carModelId = data.get(position).getId();
        BasicInfoActivity.carModelName =data.get(position).getName();
        Logger.d("carModel==="+data.get(position).getId()+ "  "+data.get(position).getName());
        setResult(RequestAndResultCode.CarModelActivity_Result);
        finish();
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }
}
